//
//  PrivacyTool.swift
//  QNN
//
//  Created by TifaTsubasa on 17/3/28.
//  Copyright © 2017年 qianshengqian. All rights reserved.
//

import Foundation
import Photos
import AVFoundation

class PrivacyTool {
  
  /// 相机权限
  class func haveSourceCamera() -> Bool {
    return UIImagePickerController.isSourceTypeAvailable(.camera)
  }
  
  /// 相册权限
  class func haveAssetsLibraryAuthorization() -> Bool {
    let author = PHPhotoLibrary.authorizationStatus()
    if author == .restricted  || author == .denied {
      return false
    }
    return true
  }
  
  /// 相机权限
  class func haveCameraAuthorization() -> Bool {
    let author = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
    if author == .restricted || author == .denied {
      return false
    }
    return true
  }
  
  /// 定位权限
  class func haveLocationAuthorization() -> Bool {
    let type = CLLocationManager.authorizationStatus()
    if type == .restricted || type == .denied {
      return false
    }
    return true
  }
  
  /// 权限弹窗
  static func popPrivacyAlertView(title: String, showVc vc: UIViewController?) {
    let alertVc = UIAlertController(title: title, message: "", preferredStyle: .alert)
    alertVc.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (_) in
      alertVc.dismiss(animated: true, completion: nil)
    }))
    alertVc.addAction(UIAlertAction(title: "设置", style: .default, handler: { (_) in
      UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
    }))
    vc?.present(alertVc, animated: true, completion: nil)
  }
}
